define(['ionic', 'utility'], function() {
    angular.module('U9Mobile.utility')
        .factory('UtilService', ['$ionicHistory', '$state', '$ionicLoading', '$ionicPopup', '$cordovaToast', '$cordovaNetwork', 'ionicU9Tips', 'AppConstants',
            function($ionicHistory, $state, $ionicLoading, $ionicPopup, $cordovaToast, $cordovaNetwork, ionicU9Tips, AppConstants) {
                var o = {};
                o.getExternalRootDir = function () {
                    return cordova.file.externalRootDirectory || cordova.file.dataDirectory;
                };
                o.getAppRootDir = function() {
                    return o.getExternalRootDir() + '/' + AppConstants.APP_NAME + '/';
                };
                o.getConfigDir = function() {
                    return o.getAppRootDir();
                };
                o.getAppsDir = function() {
                    return o.getAppRootDir() + 'apps/';
                };
                o.getMainAppDir = function() {
                    if (window.cordova && cordova.file) {
                        return cordova.file.applicationDirectory + 'www/';
                    } else {
                        return '../../../../U9V2/www/';
                    }
                };
                o.convertAppURL = function(srcURL, require) {
                    var childAppRootDir = '../../';
                    if (!require) {
                        childAppRootDir += 'FileStorage/apps/';
                    }
                    if (window.cordova && cordova.file) {
                        childAppRootDir = o.getAppsDir();
                    }
                    return childAppRootDir + srcURL;
                };

                o.showLoading = function(tips) {
                    // android,ios,bubbles,circles,crescent,dots,lines,ripple,spiral
                    var tpl = '<ion-spinner icon="circles"></ion-spinner>';
                    if (angular.isString(tips)) {
                        tpl += '<div class="tips"><span>' + tips + '</span></div>';
                    }
                    $ionicLoading.show({
                        template: tpl
                    });
                };
                o.hideLoading = function() {
                    $ionicLoading.hide();
                };

                o.alert = function(message, title) {
                    return $ionicPopup.alert({
                        title: title || '提示框',
                        okText: '确定',
                        okType: 'button-u9',
                        template: message
                    });
                };
                o.confirm = function(message, title) {
                    return $ionicPopup.confirm({
                        title: title || '消息框',
                        template: message,
                        buttons: [{
                            text: '确定',
                            type: 'button-u9',
                            onTap: function() {
                                return true;
                            }
                        }, {
                            text: '取消',
                            type: 'button-u9',
                            onTap: function() {
                                return false;
                            }
                        }]
                    });
                };

                o.toastShortCenter = function(msg) {
                    if (window.plugins && window.plugins.toast) {
                        $cordovaToast.showShortCenter(msg);
                    } else {
                        console.log(msg);
                    }
                };
                o.toastShortBottom = function(msg) {
                    if (window.plugins && window.plugins.toast) {
                        $cordovaToast.showShortBottom(msg);
                    } else {
                        console.log(msg);
                    }
                };
                o.toastLongCenter = function(msg) {
                    if (window.plugins && window.plugins.toast) {
                        $cordovaToast.showLongCenter(msg);
                    } else {
                        console.log(msg);
                    }
                };
                o.toastLongBottom = function(msg) {
                    if (window.plugins && window.plugins.toast) {
                        $cordovaToast.showLongBottom(msg);
                    } else {
                        console.log(msg);
                    }
                };
                o.tipsTop = function(tips, missable) {
                    var config = {
                        content: tips,
                        position: 'top',
                        dismissable: !missable
                    };
                    if (!missable) {
                        config.duration = 1500;
                    }
                    ionicU9Tips.show(config);
                };
                o.tipsCenter = function(tips, missable) {
                    var config = {
                        content: tips,
                        position: 'center',
                        dismissable: !missable
                    };
                    if (!missable) {
                        config.duration = 1500;
                    }
                    ionicU9Tips.show(config);
                };
                o.tipsBottom = function(tips, missable) {
                    var config = {
                        content: tips,
                        position: 'bottom',
                        dismissable: !missable
                    };
                    if (!missable) {
                        config.duration = 1500;
                    }
                    ionicU9Tips.show(config);
                };

                o.goState = function(state, enableAnimate) {
                    $ionicHistory.nextViewOptions({
                        historyRoot: true,
                        disableAnimate: !enableAnimate,
                        expire: 300
                    });
                    $state.go(state);
                };

                o.checkNetwork = function() {
                    if (navigator.connection) {
                        if ($cordovaNetwork.isOnline()) {
                            return true;
                        } else if ($cordovaNetwork.isOffline()) {
                            return false;
                        }
                    } else {
                        return true;
                    }
                };

                return o;
            }
        ]);
});
